package ui.panels;

import entities.Department;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import manager.GenericDriver;
import manager.AdminBusiDriver;
import model.ViewDepartmentComboBoxModel;
import model.ViewDoctorSchedulerTableModel;
import ui.windows.RemedySchedulerFrame;

public class ViewDoctorSchedulerPanel extends javax.swing.JPanel {

    /** Creates new form ViewDoctorSchedulerPanel */
    public ViewDoctorSchedulerPanel(RemedySchedulerFrame mainFrame) {
        this.mainFrame = mainFrame;
        genDriver = mainFrame.getGenericDriver();
        driver = (AdminBusiDriver) mainFrame.getDriver();
        initComponents();
        includeModel();
    }

    private void includeModel() {
        depModel = new ViewDepartmentComboBoxModel();
        Department dep = new Department();
        dep.setDepartmentID(0);
        dep.setName("All");
        depModel.addElement(dep);
        depModel.addAll(genDriver.showDepartment());
        cbDeps.setModel(depModel);
        cbDeps.setSelectedIndex(0);
        schedulerModel = new ViewDoctorSchedulerTableModel(driver.showViewDoctorScheduler());
        schedulerTable.setModel(schedulerModel);
        schedulerTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        schedulerTable.setFillsViewportHeight(true);
        schedulerTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        schedulerTable.getTableHeader().setFont(new Font(null, Font.BOLD, 10));
        DefaultTableCellRenderer cellRenderer1 = new DefaultTableCellRenderer();
        cellRenderer1.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        cellRenderer1.setBackground(new Color(100, 50, 102));
        cellRenderer1.setForeground(new Color(240, 240, 240));
        DefaultTableCellRenderer cellRenderer2 = new DefaultTableCellRenderer();
        cellRenderer2.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        cellRenderer2.setBackground(Color.yellow);
        schedulerTable.getColumnModel().getColumn(0).setPreferredWidth(75);
        schedulerTable.getColumnModel().getColumn(1).setPreferredWidth(170);
        schedulerTable.getColumnModel().getColumn(2).setPreferredWidth(170);
        schedulerTable.getColumnModel().getColumn(3).setPreferredWidth(110);
        schedulerTable.getColumnModel().getColumn(3).setCellRenderer(cellRenderer1);
        schedulerTable.getColumnModel().getColumn(4).setPreferredWidth(110);
        schedulerTable.getColumnModel().getColumn(4).setCellRenderer(cellRenderer2);
        schedulerTable.getColumnModel().getColumn(5).setPreferredWidth(110);
        schedulerTable.getColumnModel().getColumn(5).setCellRenderer(cellRenderer2);
    }

    private void filter() {
        Department dep = (Department) cbDeps.getSelectedItem();
        if (dep != null && schedulerModel != null) {
            schedulerModel.filter(txtID.getText(), txtName.getText(), dep.getDepartmentID(), 0);
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private RemedySchedulerFrame mainFrame;
    private GenericDriver genDriver;
    private AdminBusiDriver driver;
    private ViewDepartmentComboBoxModel depModel;
    private ViewDoctorSchedulerTableModel schedulerModel;
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtID = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        cbDeps = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        schedulerTable = new javax.swing.JTable();

        setBackground(new java.awt.Color(255, 255, 255));
        setLayout(new java.awt.BorderLayout());

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setText("ID:");

        txtID.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtIDCaretUpdate(evt);
            }
        });

        jLabel2.setText("Name:");

        txtName.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtNameCaretUpdate(evt);
            }
        });

        cbDeps.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbDeps.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbDepsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtID, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addComponent(cbDeps, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(115, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbDeps, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        add(jPanel1, java.awt.BorderLayout.PAGE_START);

        schedulerTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(schedulerTable);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

private void txtIDCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtIDCaretUpdate
    filter();
}//GEN-LAST:event_txtIDCaretUpdate

private void txtNameCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtNameCaretUpdate
    filter();
}//GEN-LAST:event_txtNameCaretUpdate

private void cbDepsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbDepsActionPerformed
    filter();
}//GEN-LAST:event_cbDepsActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbDeps;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable schedulerTable;
    private javax.swing.JTextField txtID;
    private javax.swing.JTextField txtName;
    // End of variables declaration//GEN-END:variables
}
